package com.hb.trade.server.project.opening.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hb.core.pojo.parent.PageResult;
import com.hb.core.pojo.project.calibration.entity.BidWinResult;
import com.hb.core.pojo.project.opening.dto.OpeningSetDTO;
import com.hb.core.pojo.project.opening.entity.OpeningSet;
import com.hb.core.pojo.project.section.entity.BidSection;
import com.hb.trade.server.project.opening.service.OpeningSetService;
import com.hb.trade.server.project.section.service.raw.BidSectionService;
import org.fzzn.core.model.enums.MsgEnum;
import org.fzzn.core.model.msg.Message;
import org.fzzn.core.util.MsgUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * 标段开启设置表 前端控制器
 *
 * @author zhaojk
 * @since 2021-11-16
 */
@RestController
public class OpeningSetController {

    @Autowired
    private OpeningSetService openSetService;

    @Autowired
    private BidSectionService bidSectionService;

    /**
     * 查询开标设置列表
     *
     * @param openingSet
     * @return
     */
    @PostMapping("/openingSet/pageOpeningSet")
    public Message<PageResult> pageOpeningSet(@RequestBody OpeningSetDTO openingSet) {
        if(null == openingSet.getProjectId()) {
            return MsgUtil.fail(MsgEnum.INCORRECT_PARAM.msg());
        }
        PageResult pageResult = openSetService.pageOpeningSet(openingSet);
        return MsgUtil.success(pageResult);
    }

    /**
     * 获取未选择的标段
     *
     * @param openingSet
     * @return
     */
    @PostMapping("/openingSet/getSectionNotSelected")
    public Message<PageResult> getSectionNotSelected(@RequestBody OpeningSetDTO openingSet) {
        if (null == openingSet.getProjectId()) {
            return MsgUtil.fail(MsgEnum.INCORRECT_PARAM.msg());
        }
        PageResult pageResult = openSetService.getSectionNotSelected(openingSet);
        return MsgUtil.success(pageResult);
    }

    /**
     * 设置监标人
     *
     * @param openSet
     * @return
     */
    @SuppressWarnings("rawtypes")
    @PostMapping("/openingSet/setSupervisor")
    public Message setSupervisor(@RequestBody OpeningSetDTO openSet) {
        String valid = openSet.paramsValid();
        if (StrUtil.isNotEmpty(valid)) {
            return MsgUtil.fail(valid);
        }
        openSet.setStatus(0);
        openSet.buildBaseData(1);
        boolean save = openSetService.save(openSet);
        return MsgUtil.msg(save);
    }

    /**
     * 保存开标设置
     *
     * @param openSet
     * @return
     */
    @SuppressWarnings("rawtypes")
    @PostMapping("/openingSet/save")
    @Transactional(rollbackFor = Exception.class)
    public Message save(@RequestBody OpeningSetDTO openSet) {
        String valid = openSet.paramsValid();
        if (StrUtil.isNotEmpty(valid)) {
            return MsgUtil.fail(valid);
        }
        boolean save;
        if (openSet.getId() == null) {
            openSet.buildBaseData(1);
            openSet.setStatus(0);
            save = openSetService.save(openSet);
        } else {
            save = openSetService.updateById(openSet);
            //原来关联的标段设置的标段为null
            UpdateWrapper<BidSection> update = new UpdateWrapper<>();
            update.set("opening_set_id",null).eq("opening_set_id",openSet.getId());
            bidSectionService.update(update);
        }

        //处理选择的标段信息，更新标段表opening_set_id字段
        List<BidSection> bidSectionList = openSet.getBidSectionList();
        if (CollUtil.isEmpty(bidSectionList)) {
            return MsgUtil.fail("标段未选择");
        }

        for (BidSection bidSection : bidSectionList) {
            bidSection.setOpeningSetId(openSet.getId());
        }
        bidSectionService.updateBatchById(bidSectionList);
        return MsgUtil.msg(save);
    }

    /**
     * 申请开启
     *
     * @param openSet
     * @return
     */
    @SuppressWarnings("rawtypes")
    @PostMapping("/openingSet/openApply")
    public Message openApply(@RequestBody OpeningSetDTO openSet) {
        String valid = openSet.paramsValid();
        if (StrUtil.isNotEmpty(valid)) {
            return MsgUtil.fail(valid);
        }
        if (null == openSet.getId()) {
            return MsgUtil.fail("ID不可为空");
        }
        openSet.setStatus(2);
        boolean save = openSetService.updateById(openSet);
        return MsgUtil.msg(save);
    }

    /**
     * 开标设置审核
     *
     * @param openSet
     * @return
     */
    @SuppressWarnings("rawtypes")
    @PostMapping("/openingSet/approved")
    public Message approved(@RequestBody OpeningSetDTO openSet) {
        if (null == openSet.getId()) {
            return MsgUtil.fail("ID不可为空");
        }
        boolean save = openSetService.updateById(openSet);
        return MsgUtil.msg(save);
    }

    /**
     * 详情
     *
     * @param openSet (设置主键Id或者标段Id)
     * @return
     */
    @SuppressWarnings("rawtypes")
    @PostMapping("/openingSet/detail")
    public Message<OpeningSetDTO> detail(@RequestBody OpeningSetDTO openSet) {
        OpeningSet openingSet = new OpeningSet();
        List<BidSection> bidSectionList = new ArrayList<>();
        //项目操作台根据设置主键Id查询
        if (null != openSet.getId()) {
            openingSet = openSetService.getById(openSet.getId());
            //查询对应标段
             bidSectionList= bidSectionService.list(new LambdaQueryWrapper<BidSection>()
                    .eq(BidSection :: getOpeningSetId, openSet.getId()));
        }
        //标段操作台根据标段Id查询
        if (null != openSet.getSectionId()) {
            BidSection bidSection = bidSectionService.getById(openSet.getSectionId());
            if (null == bidSection) {
                return MsgUtil.fail("未获取到相关标段信息");
            }
            if (null == bidSection.getOpeningSetId()) {
                return MsgUtil.success(new OpeningSetDTO());
            }
            openingSet = openSetService.getById(bidSection.getOpeningSetId());
            bidSectionList.add(bidSection);
        }
        OpeningSetDTO openingSetDTO = openingSet == null ? new OpeningSetDTO() :  openingSet.to(OpeningSetDTO.class);
        openingSetDTO.setBidSectionList(bidSectionList);
        return MsgUtil.success(openingSetDTO);
    }

    /**
     * 开标设置审核列表
     *
     * @param openSet
     * @return
     */
    @SuppressWarnings("rawtypes")
    @PostMapping("/openingSet/applyList")
    public Message applyList(@RequestBody OpeningSetDTO openSet) {
        QueryWrapper<OpeningSet> wrapper = openSet.buildWrapper();
        wrapper.eq("supervisor_id", openSet.getSupervisorId());
        wrapper.orderByDesc("create_time");
        Page<OpeningSet> pages = openSet.pages();
        openSetService.page(pages, wrapper);
        List<OpeningSet> list = pages.getRecords();
        return MsgUtil.success(list);
    }
}
